package com.yuwei.video

import android.app.Application
import android.content.Context
import androidx.startup.Initializer
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.tencent.mmkv.MMKV
import com.yuwei.base.BaseApp
import com.yuwei.database.RoomDBUtils
import kotlinx.coroutines.*

/**
 * SDK初始化器
 */
class SdkInitializer: Initializer<Unit> {
    //用于初始化 协程作用域
    private val mInitSDKCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    override fun create(context: Context) {
//        LogUtils.i("SdkInitializer create了${System.currentTimeMillis()}")
        mInitSDKCoroutineScope.launch {
            //执行多个任务
            val allResult = listOf(
                async {initMMVK(context)},
                async {initARouter(context)},
                async {initRoom(context)}
            )
            allResult.awaitAll()
            //等待所有的初始化任务返回
//            LogUtils.i("初始化完成,耗时:${endTime - startTime}")
        }
    }private suspend fun initMMVK(context: Context){
        withContext(Dispatchers.IO){
            MMKV.initialize(context)
//            LogUtils.i("MMKV初始化完成,路径为:${MMKV.initialize(context)}")
        }

    }
    private suspend fun initARouter(context: Context){
        withContext(Dispatchers.IO){
            if(BuildConfig.DEBUG){
                ARouter.openLog()
                ARouter.openDebug()
            }
            ARouter.init(context as Application)
//            LogUtils.i("ARouter初始化完成")
        }
    }
    private suspend fun initRoom(context: Context){
        withContext(Dispatchers.IO){
            RoomDBUtils.initRoomDB(context)
//            LogUtils.i("Room初始化完成")
        }
    }
    /**
     * 需要的依赖其他初始化器
     */
    override fun dependencies(): List<Class<out Initializer<*>>> {
        return emptyList()
    }

}